<template>
  <el-dialog title="新增部门" :visible="showDialog" @close="close">
    <!-- 放置弹层内容 -->
    <el-form ref="addDept" :model="formData" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="formData.name" placeholder="1-20个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="employeeNumber" label="部门人数">
        <el-input v-model="formData.employeeNumber" placeholder="请输入部门人数" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="password" label="部门密码">
        <el-input v-model="formData.password" placeholder="请输入部门密码" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="levelId" label="部门级别">
        <el-select v-model="formData.levelId" placeholder="请选择部门级别" style="width: 80%" size="mini">
          <!-- 下拉选项 循环 负责人数据 label表示显示的字段 value 存储字段 -->
          <el-option v-for="item in levelList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="btnOK">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getDepartment, addDepartment, getLevelList } from '@/api/department'
import {getDepartmentDetailToUpdate,updateDepartmentDetail } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    },
    // currentDepartmentName: {
    //   type: String,
    //   default: null
    // }
  },
  data() {
    return {
      levelList: [], // 存储负责人列表
      formData: {
        levelId: '', // 部门级别
        name: '', // 部门名称
        pid: '', // 父级部门的id
        password:'',
        employeeNumber: null
      },
      rules: {
        levelId: [{ required: true, message: '部门级别不能为空', trigger: 'blur' }], // 部门级别id
        employeeNumber: [{ required: true, message: '部门人数不能为空', trigger: 'blur' }], 
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1, max: 30, message: '部门名称的长度为1-20个字符', trigger: 'blur'
          }, {
            trigger: 'blur',
            // 自定义校验模式
            validator: async(rule, value, callback) => {
              // value就是输入的编码
              const result = await getDepartment()
              // result数组中是否存在 value值
              if (result.some(item => item.name === value)) {
                callback(new Error('部门中已经有该名称了'))
              } else {
                callback()
              }
            }
          }] // 部门名称
        // pid: '' // 父级部门的id 不需要做校验
      }
    }
  },
  created() {
    this.getLevelList()
    // console.log(this.currentDepartmentName)
    // console.log(this.currentNodeId)
    // this.getDepartmentDetailToUpdate(this.currentDepartmentName);
  },
  // mounted() {
  //   console.log(this.currentDepartmentName); // 输出父组件传递的部门名称
  //   console.log(this.currentNodeId); // 输出父组件传递的节点ID
  // },
  // watch:{
  //   currentDepartmentName(newValue, oldValue) {
  //     this.getDepartmentDetailToUpdate(newValue);    }
  // },
  methods: {
    close() {
      // 修改父组件的值 子传父
      // resetFields 只能重置在模板中绑定的数据
      this.formData = {
        levelId: '', // 部门负责人id
        name: '', // 部门名称
        pid: '', // 父级部门的id
        employeeNumber: null
      }
      this.$refs.addDept.resetFields() // 重置表单
      this.$emit('update:showDialog', false)
    },
    async getLevelList() {
      this.levelList = await getLevelList()
    },
    // async getDepartmentDetailToUpdate(departmentName){
    //   const data = {  name:departmentName}
    //   this.formData = await getDepartmentDetailToUpdate(data)
    //   console.log(this.formData)
    // },
    // async updateDepartmentDetail(formData){
    //
    //   await updateDepartmentDetail(formData)
    //   this.$message.success('修改成功！')
    // },
    // 点击确定时调用
    btnOK() {
      this.$refs.addDept.validate(async isOK => {
        if (isOK) {
          await addDepartment({ ...this.formData, pid: this.currentNodeId })
          // 通知父组件更新
          this.$emit('updateDepartment')
          // 提示消息
          this.close()
        }
      })
    }
  }
}
</script>
